package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.service.common.ResultService;
import com.fz.us.base.util.CommonUtil;
import com.fz.us.base.util.PinyinUtil;
import com.joint.base.bean.EnumManage;
import com.joint.base.entity.Users;
import com.joint.base.service.AuthCodeService;
import com.joint.base.service.UsersService;
import com.joint.web.action.BaseAdminAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;

/**
 * Created by dqf on 2015/4/20.
 */
@ParentPackage("mp")
public class RegisterAction extends BaseAdminAction {
    private static final long serialVersionUID = -5234982607248344967L;

    @Resource
    private UsersService usersService;
    @Resource
    private AuthCodeService authCodeService;
    @Resource
    private ResultService resultService;

    private String username;
    private String newPassword;
    private String confirmPassword;
    private String authcode;

    public String sendAuthCode(){
        return ajaxResult(usersService.sendAuthCode(username, EnumManage.AuthCodeEnum.register,1,null));
    }

    public String sendexperienceAuthCode(){

        if(!CommonUtil.isMobile(username)){
            return ajaxResult(resultService.build(0,1,"用户名必须为手机号！",null));
        }

        if(usersService.getUsersByMobile("ty"+username)!=null){
            return ajaxResult(resultService.build(0,1,"已经存在该用户",null));
        }
        Users users=new Users();
        users.setAdminType("12");
        users.setName("ty" + username);
        users.setMobile("ty" + username);
        users.setPassword("pass");
        users.setState(BaseEnum.StateEnum.Enable);
        users.setPinYin(PinyinUtil.getPingYin("ty" + username));
        users.setPinYinHead(PinyinUtil.getPinYinHeadChar("ty" + username));

        return ajaxResult(usersService.registerExperienceUser(username, users));
    }

    //体验注册
    public String experienceRegister(){
        if(!authCodeService.isValid("ty"+username, authcode)){
            return ajaxHtmlAppResult(0, "验证码输入错误！", null);
        }
        return ajaxResult(resultService.build(1, 0, "", null));
    }

    public String input(){
        return "input";
    }

    public String reset(){
        return "reset";
    }

    public String save(){
        if (!confirmPassword.equals(newPassword)){
            return ajaxHtmlCallback("400", "两次输入的密码不一致！");
        }

        if (StringUtils.isEmpty(newPassword)){
            return ajaxHtmlCallback("400", "密码不能为空！");
        }

        if(!authCodeService.isValid(username, authcode)){
            return ajaxHtmlCallback("400", "验证码错误！");
        }

        return ajaxHtmlCallback("200", "密码重置成功！");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getAuthcode() {
        return authcode;
    }

    public void setAuthcode(String authcode) {
        this.authcode = authcode;
    }
}
